package com.qf.dao.Impl;/*
    Dell
    2021/10/9
    11:32
*/

import com.qf.dao.ProductDao;
import com.qf.entiy.Category;
import com.qf.entiy.Product;
import com.qf.util.DruidUtil;
import com.qf.util.Page;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ProductDaoImpl implements ProductDao {
    private QueryRunner queryRunner = new QueryRunner();
    @Override
    public List<Product> showAll(Page page) {
        try {
            String sql = "select p.id, p.pname, c.name, date, vender, price, type, p.cid from product as p inner join category c on p.cid = c.id limit ?,?;";
            System.out.println(page.getStartRow());
            System.out.println(page.getPageSize());
            List<Map<String, Object>> query = queryRunner.query(DruidUtil.getConnection(), sql, new MapListHandler(),page.getStartRow(),page.getPageSize());
            if(query==null){
                return null;
            }
            List<Product> productList = new ArrayList<>();
            for (Map<String, Object> map : query) {
                Product product  = new Product();
                Category category  = new Category();
                BeanUtils.populate(product, map);
                BeanUtils.populate(category, map);
                product.setCategory(category);
                productList.add(product);
            }
            System.out.println(productList);
            return productList;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage()+":分页查询出现了异常");
        }
    }

    @Override
    public Long countProduct() {
        try {
            return queryRunner.query(DruidUtil.getConnection(),"select count(*) from Product;", new ScalarHandler<>());
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+":统计所有条数出现了异常");
        }
    }

    @Override
    public Integer insert(Product product, Integer cid) {
        try {
            String sql = "insert into product values (0,?,?,?,?,?,?);";
            Object[] param = {product.getPname(), product.getDate(), product.getVender(), product.getPrice(), product.getType(), cid};
            return queryRunner.update(DruidUtil.getConnection(), sql,param);
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+":新增药品出现了异常");
        }
    }

    @Override
    public Integer update(Product product, Integer cid) {
        try {
            Object[] params = {product.getPname(),product.getDate(),product.getVender(),product.getPrice(),product.getType(),cid,product.getId()};
            String sql = "update Product set pname = ?,date = ?,vender = ?,price = ?,type = ?,cid = ? where id = ?;";
            return queryRunner.update(DruidUtil.getConnection(), sql,params);
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+":修改药品信息出现了异常");
        }
    }

    @Override
    public Integer delete(Integer id) {
        try {
            return queryRunner.update(DruidUtil.getConnection(),"delete from Product where id = ?",id);
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+":删除药品出现了异常");
        }
    }
}
